﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;
using WinFormsAppTest.Dao;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace WinFormsAppTest
{
    public partial class ExamAddForm : Form
    {
        //
        public String username = "null";//设置默认登录用户(手动添加)
        public int id = 0;//设置默认登录用户(手动添加)
        //
        public ExamAddForm()
        {
            InitializeComponent();
        }

        // -------有参 窗口（手动添加）--------
        public ExamAddForm(String name, int uid)
        {
            username = name;
            id = uid;
            InitializeComponent();
        }
        //-----------------------------------

        private void OKbutton_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(StatetextBox.Text))
            {
                MessageBox.Show("添加员工奖惩情况不能为空！");
            }
            else if (string.IsNullOrWhiteSpace(MeasuretextBox.Text))
            {
                MessageBox.Show("添加员工奖惩措施不能为空！");
            }
            else if (string.IsNullOrWhiteSpace(GoaltextBox.Text))
            {
                MessageBox.Show("添加员工考核目标不能为空！");
            }
            else if (string.IsNullOrWhiteSpace(UserIdtextBox.Text))
            {
                MessageBox.Show("添加员工编号不能为空！");
            }

            AdminDao testdao = new AdminDao();
            ExamDao Examdao = new ExamDao();
            if (int.TryParse(UserIdtextBox.Text, out int number)
                && testdao.GetUserById(number).Count != 0)
            {
                //直接调用dao层进行添加
                //TestDao testdao = new TestDao();
                        Examdao.CreateExam(StatetextBox.Text, MeasuretextBox.Text,
                                          GoaltextBox.Text,number);
                        MessageBox.Show("添加成功！");
                        ExamForm f2 = new ExamForm(username, id);
                        this.Hide();
                        f2.ShowDialog();
                        this.Dispose();
                 

            }
            else
            {
                MessageBox.Show("添加员工编号不存在！");
            }
        }

        private void Exitbutton_Click(object sender, EventArgs e)
        {
            ExamForm f2 = new ExamForm(username, id);
            this.Hide();
            f2.ShowDialog();
            this.Dispose();
        }

        private void ExamAddForm_Closing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.UserClosing)
            {
                // 关闭进程
                Process.GetCurrentProcess().Kill();
            }
        }
    }
}
